﻿using System;

namespace QuickMenus
{
    /// <summary>
    /// A menu item that always succeeds, and marks a end-user's intention to exit a menu or sub menu.
    /// </summary>
    public sealed class ExitItem : MenuItem
    {
        private static readonly Func<string[], int> defaultAction;

        /// <summary>
        /// In most cases, little granularity is required for menu items to allow navigation up a tree.
        /// <see cref="DEFAULT_INSTANCE"/> allows one to reduce memory consumption by having only one instance of an ExitItem.
        /// </summary>
        public static readonly Lazy<ExitItem> DEFAULT_INSTANCE;

        /// <summary>
        /// In most cases, little granularity is required for menu items to allow navigation up a tree.
        /// <see cref="BACK_INSTANCE"/> allows one to reduce memory consumption by having only one instance of an ExitItem.
        /// </summary>
        public static readonly Lazy<ExitItem> BACK_INSTANCE;

        /// <summary>
        /// The text that will be displayed by default to represent the action of exiting a top-level menu.
        /// </summary>
        public const string DEFAULT_NAME = "Exit";

        /// <summary>
        /// The text that will be displayed by default to represent that action of exiting a sub-menu.
        /// </summary>
        public const string GO_BACK = "Go Back";

        static ExitItem()
        {
            defaultAction = (string[] t) => { return 0; };
            DEFAULT_INSTANCE = new Lazy<ExitItem>(() => new ExitItem(DEFAULT_NAME), true);
            BACK_INSTANCE = new Lazy<ExitItem>(() => new ExitItem(GO_BACK), true);
        }

        public ExitItem(string name = DEFAULT_NAME)
            : base(name)
        {
            //Intentionally Left Blank
        }

        public override Func<string[], int> Operation
        {
            get
            {
                return defaultAction;
            }
        }
    }
}
